package com.heima.tess4j;

import net.sourceforge.tess4j.ITesseract;
import net.sourceforge.tess4j.Tesseract;
import net.sourceforge.tess4j.TesseractException;

import java.io.File;

/**
 * OCR图片文字识别
 */
public class Application {
    public static void main(String[] args) throws TesseractException {

        //获取本地图片
        File file = new File("D:\\leadnews-2021\\image-1.png");
        //创建ITesseract对象
        ITesseract iTesseract = new Tesseract();
        //设置字体库路径
        iTesseract.setDatapath("D:\\leadnews-2021");
        //设置识别的语言
        iTesseract.setLanguage("chi_sim");
        //执行OCR识别
        String doOCR = iTesseract.doOCR(file);
        //替换回车和tal键  使结果为一行
        String replaceAll = doOCR.replaceAll("\\r|\\n", "-").replaceAll(" ", "");
        System.out.println(replaceAll);

    }
}
